﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.Ports;
using System.Threading;
using System.Web;
using System.Net;
using System.Management;
using System.Net.Sockets;

namespace comm
{
    public partial class simsend : Form
    {
        System.Timers.Timer t12 = new System.Timers.Timer(4000);
        BLL.tbl_colseAlarm b_Close_Alarm = new BLL.tbl_colseAlarm();
        BLL.tbl_openAlarm b_Open_Alarm = new BLL.tbl_openAlarm();
        BLL.tbl_ContactPerson b_Contact = new BLL.tbl_ContactPerson();
        public simsend()
        {
            InitializeComponent();
        }

        private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            SendSMS();
            SendCancelSMS();
        }


        private void button3_Click(object sender, EventArgs e) //打开短信端口
        {
            //int port = int.Parse(comboBox1.Text);

        }

        protected void simstart()
        {
            int port = 3;
            string str = "9600,N,8,1";
            string sN = "868995833293722";
            //textBox4
            //打开端口
            label5.Text = "初始化端口...";
            string ret = axSzhtoSms1.YhOpenModem(port, str, sN);

            if (ret.IndexOf("-1") > 0)
            {
                label5.Text = "端口失败";
            }
            else
            {
                label2.Text = ret;
                label5.Text = "端口成功";
            }
        }





        private void simsend_Load(object sender, EventArgs e)
        {
            //string[] Port = axSzhtoSms1.YhGetComPort().Split(',');
            //for (int i = 0; i < Port.Length; i++)
            //    comboBox1.Items.Add(Port[i]);
            //comboBox1.Text = Port[0];
            simstart();

            t12.Start();
            t12.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
            t12.AutoReset = true;
            t12.Enabled = true;
            Thread.Sleep(500);
            this.Hide();
        }

        protected void SendSMS() //发送短信警报表
        {
            DataTable opsimdata = b_Open_Alarm.GetList("").Tables[0];
            DataTable simpreson = b_Contact.GetList("").Tables[0];

            for (int i = 0; i < opsimdata.Rows.Count; i++) //循环待发送警报表
            {
                if (opsimdata.Rows.Count > 0) //判断短信报警表是否有报警数据
                {
                    string simcontent = opsimdata.Rows[i]["AlarmContent"].ToString();
                    int oid = Convert.ToInt32(opsimdata.Rows[i]["id"].ToString());
                    for (int ii = 0; ii < simpreson.Rows.Count; ii++) //遍历短信接收人表
                    {
                        axSzhtoSms1.YhSendSms(label2.Text, simpreson.Rows[ii]["Phone"].ToString().Trim(), simcontent, 1);
                        Thread.Sleep(300);
                    }
                    b_Open_Alarm.Delete(oid);
                }
            }
        }

        protected void SendCancelSMS()//发送取消短信警报表
        {
            DataTable clsimdata = b_Close_Alarm.GetList("").Tables[0];
            DataTable simpreson = b_Contact.GetList("").Tables[0];
            for (int j = 0; j < clsimdata.Rows.Count; j++) //循环待发送取消报警表
            {
                if (clsimdata.Rows.Count > 0) //判断短信取消报警表是否有数据
                {
                    string simcontent = clsimdata.Rows[j]["AlarmContent"].ToString();
                    int oid = Convert.ToInt32(clsimdata.Rows[j]["id"].ToString());
                    for (int jj = 0; jj < simpreson.Rows.Count; jj++) //遍历短信接收人表
                    {
                        axSzhtoSms1.YhSendSms(label2.Text, simpreson.Rows[jj]["Phone"].ToString().Trim(), simcontent, 1);
                        Thread.Sleep(300);
                    }
                    b_Close_Alarm.Delete(oid);
                }
            }
        }

 

        private void button4_Click(object sender, EventArgs e)
        {
            //Form1 f1 = new Form1();
            this.Hide();
        }
    }
}
